package com.nlx.notes.module.mapper;

import com.nlx.notes.module.bean.dto.querydb.FlowMonthQueryDbDTO;
import com.nlx.notes.module.entity.FlowMonthStatistics;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author nlx
 * @since 2024-12-15
 */
public interface FlowMonthStatisticsMapper extends BaseMapper<FlowMonthStatistics> {

    @Update(" update flow_month_statistics set expense = expense +#{monthAmount} where id = #{id}")
    void incMonthExpenseAmount(@Param("id") Long id, @Param("monthAmount") int monthAmount);

    @Update(" update flow_month_statistics set income = income +#{monthAmount} where id = #{id}")
    void incMonthIncomeAmount(@Param("id") Long id, @Param("monthAmount") int monthAmount);


    @Update(" update  flow_month_statistics set month_amount = month_amount - #{amount} where id = #{id} ")
    void decMonthAmount(@Param("id") Long id, @Param("amount") int amount);

    List<FlowMonthStatistics> findByCondition(FlowMonthQueryDbDTO flowMonthQueryDbDTO);

    @Select(" select * from flow_month_statistics where year_date = #{year} and user_id = #{userId} and book_id = #{bookId} and expense > 0  and del_flag = 0 order by expense desc limit 1")
    FlowMonthStatistics findExpenseMaxAmountByYear(@Param("year") String year, @Param("bookId") Long bookId, @Param("userId") Long userId);

    @Select(" select * from flow_month_statistics where year_date = #{year} and user_id = #{userId} and book_id = #{bookId} and income > 0 and del_flag = 0 order by income desc limit 1")
    FlowMonthStatistics findIncomeMaxAmountByYear(@Param("year") String year, @Param("bookId") Long bookId, @Param("userId") Long userId);
}
